package com.j.sdks.bll.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.j.sdks.bll.PageBean;
import com.j.sdks.bll.SearchResult;
import com.j.sdks.bll.persistence.BaseDataAccess;
import com.j.sdks.bll.service.BaseService;

@Component("serviceTemplateImpl")
@Transactional
public class BaseServiceImpl<T, PK extends java.io.Serializable> implements
		BaseService<T, PK> {
	protected BaseDataAccess<T, PK> dataAccessTemplate;

	@Autowired
	public BaseServiceImpl(
			@Qualifier("dataAccessTemplateImpl") BaseDataAccess dataAccessTemplate) {
		this.dataAccessTemplate = dataAccessTemplate;
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int insert(T obj) {
		return dataAccessTemplate.insert(obj);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int delete(PK pk) {
		return dataAccessTemplate.delete(pk);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int update(T obj) {
		return dataAccessTemplate.update(obj);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int merge(T obj) {
		return dataAccessTemplate.merge(obj);
	}

	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public T get(PK pk) {
		return dataAccessTemplate.get(pk);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int batch(T[] objs) {
		return dataAccessTemplate.batch(objs);
	}

	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public boolean exist(PK pk) {
		return dataAccessTemplate.exist(pk);
	}

	public SearchResult<T> search(T obj, PageBean pageBean) {
		return dataAccessTemplate.search(obj, pageBean);
	}

}
